package ip.command;

import java.util.Collection;

import ip.dto.CategoryDTO;
import ip.ejb.session.facade.CategoryFacadeLocal;
import ip.ejb.session.facade.CategoryFacadeLocalHome;
import ip.exceptions.NoSuchUserException;
import ip.exceptions.UnexpectedException;
import ip.util.EJBHomeFactory;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.RemoveException;
import javax.naming.NamingException;
import javax.servlet.ServletException;

import org.apache.log4j.Logger;

import com.sun.java.swing.plaf.windows.WindowsTreeUI.CollapsedIcon;

public class GetCategoriesCommand extends Command{

	private Collection categories;
	private CategoryFacadeLocal categoryFacadeLocal;
	Logger logger = Logger.getLogger(this.getClass());

	public void init() throws UnexpectedException{
		try {
			logger.debug("init()");
			CategoryFacadeLocalHome categoryFacadeLocalHome = (CategoryFacadeLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.session.facade.CategoryFacadeLocalHome.class);
			categoryFacadeLocal = categoryFacadeLocalHome.create();
		} catch (NamingException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();			
		} catch (CreateException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();
		}

	}
	public void execute() throws UnexpectedException{
		try{
			logger.debug("execute()");
			categories = categoryFacadeLocal.getAllCategories();
		}
		catch (Exception e) {
			logger.error(e);
			throw new UnexpectedException();
		}
	}

	public void dispose() throws UnexpectedException{
		try {
			logger.debug("dispose()");
			categoryFacadeLocal.remove();
		} catch (EJBException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();
		} catch (RemoveException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();
		}
	}

	public Collection getCategories() {
		return categories;
	}

}
